<?php
namespace app\admin\controller;
use mylib\Delimg;

class Cosmetology extends Init
{
    public function index()
    {
        $cosmetology = db('cosmetology')->select();
        $this->assign("cosmetology", $cosmetology);
        return view();
    }

    public function add(){
        if(request()->isPost()){
            $data['co_content'] = input('co_content');

            $file = request()->file('image');
            $upload_path = config('upload.path');

            $info = $file->rule('uniqid')->move($upload_path);
            if($info){
//                var_dump($info);exit;
                $savename = $info->getSaveName();
            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
            $data['co_img'] = $savename;

            if(db('cosmetology')->insert($data)){
                $this->redirect('cosmetology/index');
            }else{
                return $this->error('添加失败！');
            }
        }

    }

    public function edit(){
        $id = input('id');
        $cosmetology = db('cosmetology')->where('co_id',$id)->find();
//        var_dump($informatioi);
        $this->assign("cosmetology", $cosmetology);

        if(request()->isPost()){
            $data['co_content'] = input('co_content');

            //删除原图片
            $delimg = new Delimg();
            $delimg->delupimg('cosmetology','co_id',$id,'co_img',".jpg",'_250_200.jpg');

            //增加新的图片
            $file = request()->file('image');
            $upload_path = config('upload.path');

            $info = $file->rule('uniqid')->move($upload_path);
            if($info){
                $savename = $info->getSaveName();
            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
            $data['co_img'] = $savename;


            if(db('cosmetology')->where('co_id',$id)->update($data)){
                $this->redirect('cosmetology/index');
//                return $this->success('修改成功！',url('informatioi/index'));
            }else{
                return $this->error('修改失败！');
            }
        }
        return view();
    }


    public function del(){
        $id = input('id');
        $delimg = new Delimg();
        $delimg->delupimg('cosmetology','co_id',$id,'co_img',".jpg",'_250_200.jpg');


        if(db('cosmetology')->where('co_id',$id)->delete()){
            $this->redirect('cosmetology/index');
        }else{
            return $this->error('删除失败！');
        }
    }


    public function delall(){
        $data = input('post.');

        $arrdel = $data['idarr'];
        $delimg = new Delimg();
        $delimg->delAllimg('cosmetology','co_id',$arrdel,'co_img',".jpg",'_250_200.jpg');

        if(request()->isPost()) {
            foreach ($arrdel as $id){
                db('cosmetology')->where('co_id',$id)->delete();
            }
            $this->redirect('cosmetology/index');
        }
    }
}